﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;

namespace devtm.Editor.Configuration.Data
{
    public class TemplateHost: ConfigProperty
    {

        public TemplateHost() { }

        public TemplateHost(PropertyInfo propertyinfo, ElementSection instance) : base(propertyinfo, instance)
        {
            Validator(Value);
        }

        /// <summary>
        /// Runs the editor.
        /// </summary>
        public override void RunEditor() { }


        public override void Selector() { }


        readonly Regex _ipv6 = new Regex(@"^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))$");
        readonly Regex _ipv4 = new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}");
        readonly Regex _host = new Regex(@"^[a-z][\w._-]*");
        public override sealed void Validator(Object o)
        {

            
            if (_ipv4.IsMatch(o.ToString()))
            {
                FormatHost = "(Ipv4)";
                IsInError = false;
            }
            else if (_ipv6.IsMatch(o.ToString()))
            {
                FormatHost = "(Ipv6)";
                IsInError = false;
            }
            else if (_host.IsMatch(o.ToString()))
            {
                FormatHost = "(Host)";
                IsInError = false;
            }
            else
            {
                FormatHost = "(unknown)";
                IsInError = true;
            }

            //base.Validator(o.ToString());


        }

        private String _FormatHost;
        public String FormatHost
        {
            get
            {
                return _FormatHost;
            }
            set
            {
                if (_FormatHost == value) return;
                _FormatHost = value;

                MyPropertyChanged("FormatHost");
            }
        }


        public override sealed Object Value
        {
            get
            {
                return base.Value;
            }
            set
            {
                int k = 0;
                if (int.TryParse(value.ToString(), out k))
                    base.Value = k;

            }
        }


    }
}
